(function ($) {
    $.fn.extend({
        /* 按键盘实现插入内容 */
        shortcuts: function () {
            this.keydown(function (e) {
                var _this = $(this);
                e.stopPropagation();
                if (e.altKey) {
                    switch (e.keyCode) {
                        //Demo
                        // case 67:
                        //     _this.insertContent('[code]' + _this.selectionRange() + '[/code]');
                        //     break;
                    }
                }
            });
        },
        /* 插入内容 */
        insertContent: function (myValue, t) {
            var $t = $(this)[0];
            if (document.selection) {
                this.focus();
                var sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
                sel.moveStart('character', -l);
                var wee = sel.text.length;
                if (arguments.length == 2) {
                    var l = $t.value.length;
                    sel.moveEnd('character', wee + t);
                    t <= 0 ? sel.moveStart('character', wee - 2 * t - myValue.length) : sel.moveStart('character', wee - t - myValue.length);
                    sel.select();
                }
            } else if ($t.selectionStart || $t.selectionStart == '0') {
                var startPos = $t.selectionStart;
                var endPos = $t.selectionEnd;
                var scrollTop = $t.scrollTop;
                $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
                this.focus();
                $t.selectionStart = startPos + myValue.length;
                $t.selectionEnd = startPos + myValue.length;
                $t.scrollTop = scrollTop;
                if (arguments.length == 2) {
                    $t.setSelectionRange(startPos - t, $t.selectionEnd + t);
                    this.focus();
                }
            } else {
                this.value += myValue;
                this.focus();
            }
        },
        /* 选择 */
        selectionRange: function (start, end) {
            var str = '';
            var thisSrc = this[0];
            if (start === undefined) {
                if (/input|textarea/i.test(thisSrc.tagName) && /firefox/i.test(navigator.userAgent)) str = thisSrc.value.substring(thisSrc.selectionStart, thisSrc.selectionEnd);
                else if (document.selection) str = document.selection.createRange().text;
                else str = document.getSelection().toString();
            } else {
                if (!/input|textarea/.test(thisSrc.tagName.toLowerCase())) return false;
                end === undefined && (end = start);
                if (thisSrc.setSelectionRange) {
                    thisSrc.setSelectionRange(start, end);
                    this.focus();
                } else {
                    var range = thisSrc.createTextRange();
                    range.move('character', start);
                    range.moveEnd('character', end - start);
                    range.select();
                }
            }
            if (start === undefined) return str;
            else return this;
        }
    });
})(jQuery);

$(function () {
    /* 增加自定义功能 */
    const items = [
        {
            title: '下载按钮',
            id: 'wmd-down-button',
            svg: '<svg t="1646913625416" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2031" width="16" height="16"><path d="M1022.955204 522.570753c0 100.19191-81.516572 181.698249-181.718715 181.698249l-185.637977 0c-11.2973 0-20.466124-9.168824-20.466124-20.466124 0-11.307533 9.168824-20.466124 20.466124-20.466124l185.637977 0c77.628008 0 140.786467-63.148226 140.786467-140.766001 0-77.423347-62.841234-140.448776-140.203182-140.766001-0.419556 0.030699-0.818645 0.051165-1.217734 0.061398-5.945409 0.143263-11.686157-2.292206-15.687284-6.702656-4.001127-4.400217-5.894244-10.335393-5.167696-16.250102 1.330298-10.806113 1.944282-19.760043 1.944282-28.192086 0-60.763922-23.658839-117.884874-66.617234-160.833035-42.968627-42.968627-100.089579-66.617234-160.843268-66.617234-47.368844 0-92.742241 14.449084-131.208321 41.781592-37.616736 26.738991-65.952084 63.700811-81.925894 106.884332-2.425236 6.538927-8.012488 11.399631-14.827707 12.893658-6.815219 1.483794-13.927197-0.603751-18.859533-5.54632-19.289322-19.330254-44.943608-29.972639-72.245418-29.972639-56.322773 0-102.146425 45.813419-102.146425 102.125959 0 0.317225 0.040932 0.982374 0.092098 1.627057 0.061398 0.920976 0.122797 1.831718 0.153496 2.762927 0.337691 9.465582-5.863545 17.928325-15.001669 20.455891-32.356942 8.933463-61.541635 28.550243-82.181721 55.217602-21.305235 27.516704-32.571836 60.508096-32.571836 95.41307 0 86.244246 70.188572 156.422585 156.443052 156.422585l169.981393 0c11.2973 0 20.466124 9.15859 20.466124 20.466124 0 11.2973-9.168824 20.466124-20.466124 20.466124l-169.981393 0c-108.828614 0-197.3753-88.536452-197.3753-197.354833 0-44.053332 14.223956-85.712127 41.126676-120.473839 22.809495-29.460985 53.897537-52.086285 88.710414-64.816215 5.065366-74.322729 67.149353-133.2447 142.751215-133.2447 28.386514 0 55.504128 8.217149 78.651314 23.52581 19.657712-39.868009 48.842405-74.169233 85.497233-100.212376 45.434795-32.295544 99.004875-49.354058 154.918325-49.354058 71.692832 0 139.087778 27.915793 189.782368 78.600149 50.694589 50.694589 78.610382 118.089535 78.610382 189.782368 0 3.704368-0.102331 7.470135-0.296759 11.368932C952.633602 352.568894 1022.955204 429.511287 1022.955204 522.570753z" p-id="2032" fill="#a7a7a7"></path><path d="M629.258611 820.711014l-102.023628 102.013395c-3.990894 4.001127-9.230222 5.996574-14.46955 5.996574s-10.478655-1.995447-14.46955-5.996574l-102.023628-102.013395c-7.992021-7.992021-7.992021-20.947078 0-28.939099s20.947078-8.002254 28.939099 0l67.087954 67.077721 0-358.699522c0-11.2973 9.15859-20.466124 20.466124-20.466124 11.307533 0 20.466124 9.168824 20.466124 20.466124l0 358.699522 67.087954-67.077721c7.992021-8.002254 20.947078-7.992021 28.939099 0S637.250632 812.718993 629.258611 820.711014z" p-id="2033" fill="#a7a7a7"></path></svg>',
            text: '\n{down url=""}\n'
        },
        {
            title: '手风琴面板',
            id: 'wmd-accordion-button',
            svg: '<svg t="1646916837699" class="icon" viewBox="0 0 1280 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3478" width="16" height="16"><path d="M1017.7792 326.5408c0 19.4176-13.2736 35.1488-29.6704 35.1488H279.4368c-16.3968 0-29.6576-15.7312-29.6576-35.1488v-82.432c0-19.4304 13.2608-35.1488 29.6576-35.1488h708.6592c16.3968 0 29.6704 15.7312 29.6704 35.1488v82.432z" fill="#a7a7a7" p-id="3479"></path><path d="M1017.7792 303.104c0 19.4176-13.2736 35.1488-29.6704 35.1488H279.4368c-16.3968 0-29.6576-15.7312-29.6576-35.1488v-82.432c0-19.4176 13.2608-35.1488 29.6576-35.1488h708.6592c16.3968 0 29.6704 15.7312 29.6704 35.1488v82.432z" fill="#a7a7a7" p-id="3480"></path><path d="M310.7968 229.6576v55.6544l30.5536-27.8272z" fill="#a7a7a7" p-id="3481"></path><path d="M1017.7792 569.664c0 19.4304-13.2736 35.1616-29.6704 35.1616H279.4368c-16.3968 0-29.6576-15.7312-29.6576-35.1616v-82.4192c0-19.4304 13.2608-35.1488 29.6576-35.1488h708.6592c16.3968 0 29.6704 15.7312 29.6704 35.1488v82.4192z" fill="#a7a7a7" p-id="3482"></path><path d="M1017.7792 546.24c0 19.4176-13.2736 35.1488-29.6704 35.1488H279.4368c-16.3968 0-29.6576-15.7312-29.6576-35.1488v-82.432c0-19.4304 13.2608-35.1488 29.6576-35.1488h708.6592c16.3968 0 29.6704 15.7312 29.6704 35.1488v82.432z" fill="#a7a7a7" p-id="3483"></path><path d="M310.7968 472.7936v55.6544l30.5536-27.8272z" fill="#a7a7a7" p-id="3484"></path><path d="M1017.7792 815.7312c0 19.4304-13.2736 35.1616-29.6704 35.1616H279.4368c-16.3968 0-29.6576-15.7312-29.6576-35.1616V733.312c0-19.4304 13.2608-35.1488 29.6576-35.1488h708.6592c16.3968 0 29.6704 15.7312 29.6704 35.1488v82.4192z" fill="#a7a7a7" p-id="3485"></path><path d="M1017.7792 792.2944c0 19.4176-13.2736 35.1488-29.6704 35.1488H279.4368c-16.3968 0-29.6576-15.7312-29.6576-35.1488v-82.432c0-19.4176 13.2608-35.1488 29.6576-35.1488h708.6592c16.3968 0 29.6704 15.7312 29.6704 35.1488v82.432z" fill="#a7a7a7" p-id="3486"></path><path d="M310.7968 718.8608v55.6544l30.5536-27.8272z" fill="#a7a7a7" p-id="3487"></path></svg>',
            text: '\n{accordion}\n{accordion-item id="1" title="默认标题"}\n此处写内容\n{/accordion-item}\n{/accordion}\n'
        },
        {
            title: '本地音乐',
            id: 'wmd-music-button',
            svg: '<svg t="1647060640912" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2222" width="16" height="16"><path d="M512 640c107.314286 0 194.285714-85.942857 194.285714-192V192c0-106.057143-86.971429-192-194.285714-192s-194.285714 85.942857-194.285714 192v256c0 106.057143 86.971429 192 194.285714 192z m377.142857-194.285714c0-5.028571-4.114286-9.142857-9.142857-9.142857h-68.571429c-5.028571 0-9.142857 4.114286-9.142857 9.142857 0 160.342857-129.942857 290.285714-290.285714 290.285714S221.714286 606.057143 221.714286 445.714286c0-5.028571-4.114286-9.142857-9.142857-9.142857h-68.571429c-5.028571 0-9.142857 4.114286-9.142857 9.142857 0 192.8 144.685714 351.885714 331.428571 374.4V937.142857H300.228571c-15.657143 0-28.228571 16.342857-28.228571 36.571429v41.142857c0 5.028571 3.2 9.142857 7.085714 9.142857h465.828572c3.885714 0 7.085714-4.114286 7.085714-9.142857v-41.142857c0-20.228571-12.571429-36.571429-28.228571-36.571429H553.142857V820.685714c188.914286-20.571429 336-180.571429 336-374.971428z" p-id="2223" fill="#a7a7a7"></path></svg>',
            text: '\n{music url="音乐地址"}'
        },
    ];
    $('#wmd-button-row').append("<li class='wmd-spacer wmd-spacer5' id='wmd-spacer5'></li>");
    items.forEach(_ => {
        let item = $(`<li class="wmd-button" id="${_.id}" title="${_.title}">${_.svg}</li>`);
        item.on('click', function () {
            $('#text').insertContent(_.text);
        });
        $('#wmd-button-row').append(item);
    });
})